package com.cnblsp2.android.study.dbandio.files;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.cnblsp2.android.study.dbandio.R;

public class SDFileListActivity extends Activity {

	private ListView listView = null;
	private TextView textView = null;
	private Button parentBtn = null;

	// 记录当前的父文件夹
	private File currentParent = null;

	// 记录当前路径下的所有文件数组
	private File[] currentFiles = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_sdfilelist);

		listView = (ListView) this.findViewById(R.id.activity_sdfilelist_list_lv);
		textView = (TextView) this.findViewById(R.id.activity_sdfilelist_path_tv);
		parentBtn = (Button) this.findViewById(R.id.activity_sdfilelist_parent_btn);

		// 获取系统的SD卡目录
		File root = new File("/mnt/sdcard/");

		// 如果SD卡存在
		if (root.exists()) {
			currentParent = root;
			currentFiles = root.listFiles();
			inflateListView(currentFiles);
		}

		// 为ListView的列表项的单击事件绑定监听器
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if (currentFiles[position].isFile()) {
					return;
				}

				// 获取用户点击的文件夹下的所有文件
				File[] temp = currentFiles[position].listFiles();
				if (temp == null || temp.length == 0) {
					Toast.makeText(SDFileListActivity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_SHORT).show();
				} else {
					currentParent = currentFiles[position];
					currentFiles = temp;
					inflateListView(currentFiles);
				}

			}
		});

		// 获取上一级目录的按钮
		parentBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					if (!currentParent.getCanonicalPath().equals("/mnt/sdcard")) {
						// 获取上一级目录
						currentParent = currentParent.getParentFile();
						// 列出当前目录下所有文件
						currentFiles = currentParent.listFiles();
						inflateListView(currentFiles);
					} else {
						Toast.makeText(SDFileListActivity.this, "已经是SD根目录.", Toast.LENGTH_SHORT).show();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * 更新ListView
	 * @param files
	 */
	private void inflateListView(File[] files) {
		// 创建一个List集合，List集合的元素为Map
		List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < files.length; i++) {
			Map<String, Object> listItem = new HashMap<String, Object>();
			if (files[i].isDirectory()) {
				listItem.put("icon", R.drawable.folders);
			} else {
				listItem.put("icon", R.drawable.blue_document_horizontal);
			}
			listItem.put("fileName", files[i].getName());
			listItems.add(listItem);
		}

		// 创建一个SimpleAdapter
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.activity_sdfilelist_line, new String[] { "icon", "fileName" },
				new int[] { R.id.activity_sdfilelist_icon, R.id.activity_sdfilelist_file_name });

		listView.setAdapter(simpleAdapter);
		try {
			textView.setText("当前路径:" + currentParent.getCanonicalPath());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
